<?php
/**
 * Handler File Class
 *
 * @author liliang <liliang@wolive.cc>
 * @email liliang@wolive.cc
 * @date 2017/06/01
 */

namespace app\mobile\controller;

use think\Controller;
use app\mobile\model\User;
use app\Common;


/**
 *
 * 前台手机端控制器.
 * Class Index
 * @package app\mobile\controller
 */
class Index extends Controller
{
    /**
    * 唯一随机数方法
    * [rand description]
    * @param  [type] $len [description]
    * @return [type]      [description]
    */
   public function rand($len)
   {
        $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string=substr(time(),-3);
        for(;$len>=1;$len--)
        {
            $position=rand()%strlen($chars);
            $position2=rand()%strlen($string);
            $string=substr_replace($string,substr($chars,$position,1),$position2,0);
        }
        return $string;
    }


     /**
    * 
    * [home description]
    * @return [type] [description]
    */
   public function home(){

    $data=$this->request->request();

    if(!isset($post['product'])){
        $post['product'] ="";
    }


    $str ="visiter_id=".$data['visiter_id']."&visiter_name=".$data['visiter_name']."&avatar=".$data['avatar']."&business_id=".$data['business_id']."&groupid=".$data['groupid']."&product=".$data['product'];

    $common=new Common();

    $newstr =$common->encrypt($str, 'E', 'wolive');
    
    $a =urlencode($newstr);
    
    $this->redirect('/mobile/index?code='.$a);

   }




    /**
     *
     * 手机端首页.
     *
     * @return mixed
     */
    public function index()
    {

        $url = domain;
        $arr =$this->request->get();

        $common=new Common();

        $data =$common->encrypt($arr['code'],'D','wolive');

        if(!$data){
          $this->redirect('index/index/errors');
        }

        parse_str($data,$arr2);

        if(!isset($arr2['visiter_id']) || !isset($arr2['visiter_name']) || !isset($arr2['product']) || !isset($arr2['groupid']) || !isset($arr2['business_id']) || !isset($arr2['avatar'])){
           $this->redirect('index/index/errors');
        }

        
       $business_id =htmlspecialchars($arr2['business_id']);
 
       if(!isset($_COOKIE['visiter_id'])){

           if($arr2['visiter_id']){
              $visiter_id=htmlspecialchars($arr2['visiter_id']);
           }else{
              $visiter_id=$this->rand(2);
           }
           setcookie("visiter_id",$visiter_id,time()+3600*12);
           
           if(!isset($_COOKIE['product_id'])){
              // 没有product_id
              if($arr2['product']){
                  $product=$arr2['product'];
                  $content =json_decode($arr2['product'], true); 
                  setcookie("product_id",$content['pid'],time()+3600*12);
                  $arr2['timestamp'] =time();

                  $service =User::table('wolive_queue')->where(['visiter_id'=>$visiter_id,'business_id'=>$business_id])->find();

                   if($service){
                     $service_id =$service['service_id'];
                   }else{
                     $service_id =0;
                   }

                    $str ='<a href="'.$content['url'].'" target="_blank" class="wolive_product">';
                    $str .='<div class="wolive_img"><img src="'.$content['img'].'" width="100px"></div>';
                    $str .='<div class="wolive_head"><p>'.$content['title'].'<span class="wolive_price">'.$content['price'].'</span><p>';
                    $str .='<p class="wolive_info">'.$content['info'].'</p>';
                    $str .='</div></a>';
                  

                  $mydata =['service_id'=>$service_id,'visiter_id'=>$visiter_id,'content'=>$str,'timestamp'=>time(),'business_id'=>$business_id,'direction'=>'to_service'];

                  $chats =User::table('wolive_chats')->insert($mydata);

              }
           }else{

             $pid =$_COOKIE['product_id'];


           
             if($arr2['product']){
                $product=$arr2['product'];
                $content =json_decode($arr2['product'], true); 

                if($content['pid'] != $pid){

                    $service =User::table('wolive_queue')->where(['visiter_id'=>$visiter_id,'business_id'=>$business_id])->find();

                     if($service){
                       $service_id =$service['service_id'];
                     }else{
                       $service_id =0;
                     }

                    $str ='<a href="'.$content['url'].'" target="_blank" class="wolive_product">';
                    $str .='<div class="wolive_img"><img src="'.$content['img'].'" width="100px"></div>';
                    $str .='<div class="wolive_head"><p>'.$content['title'].'<span class="wolive_price">'.$content['price'].'</span><p>';
                    $str .='<p class="wolive_info">'.$content['info'].'</p>';
                    $str .='</div></a>';

                    $mydata =['service_id'=>$service_id,'visiter_id'=>$visiter_id,'content'=>$str,'timestamp'=>time(),'business_id'=>$business_id,'direction'=>'to_service'];
                    $chats =User::table('wolive_chats')->insert($mydata);
                    
                }
             }
           }

        }else{
           
           $visiter_id =$_COOKIE['visiter_id'];

           


           if(!isset($_COOKIE['product_id'])){
              // 没有product_id
              if($arr2['product']){
                  $product=$arr2['product'];
                  $content =json_decode($arr2['product'], true); 
                  setcookie("product_id",$content['pid'],time()+3600*12);
                  $arr2['timestamp'] =time();

                  $service =User::table('wolive_queue')->where(['visiter_id'=>$visiter_id,'business_id'=>$business_id])->find();

                   if($service){
                     $service_id =$service['service_id'];
                   }else{
                     $service_id =0;
                   }

                    $str ='<a href="'.$content['url'].'" target="_blank" class="wolive_product">';
                    $str .='<div class="wolive_img"><img src="'.$content['img'].'" width="100px"></div>';
                    $str .='<div class="wolive_head"><p>'.$content['title'].'<span class="wolive_price">'.$content['price'].'</span><p>';
                    $str .='<p class="wolive_info">'.$content['info'].'</p>';
                    $str .='</div></a>';
                  

                  $mydata =['service_id'=>$service_id,'visiter_id'=>$visiter_id,'content'=>$str,'timestamp'=>time(),'business_id'=>$business_id,'direction'=>'to_service'];

                  $chats =User::table('wolive_chats')->insert($mydata);
                 
              }
           }else{

             $pid =$_COOKIE['product_id'];
           
             if($arr2['product']){
                $product=$arr2['product'];
                $content =json_decode($arr2['product'], true); 

                if($content['pid'] != $pid){

                    $service =User::table('wolive_queue')->where(['visiter_id'=>$visiter_id,'business_id'=>$business_id])->find();

                     if($service){
                       $service_id =$service['service_id'];
                     }else{
                       $service_id =0;
                     }

                    $str ='<a href="'.$content['url'].'" target="_blank" class="wolive_product">';
                    $str .='<div class="wolive_img"><img src="'.$content['img'].'" width="100px"></div>';
                    $str .='<div class="wolive_head"><p>'.$content['title'].'<span class="wolive_price">'.$content['price'].'</span><p>';
                    $str .='<p class="wolive_info">'.$content['info'].'</p>';
                    $str .='</div></a>';
                  

                    $mydata =['service_id'=>$service_id,'visiter_id'=>$visiter_id,'content'=>$str,'timestamp'=>time(),'business_id'=>$business_id,'direction'=>'to_service'];
                    $chats =User::table('wolive_chats')->insert($mydata);
                }
             }
           }
        }




        $channel =bin2hex($visiter_id.'/'.$business_id);
        $visiter_name =htmlspecialchars($arr2['visiter_name']);
         if(isset($_SERVER['HTTP_REFERER'])){
             $from_url=$_SERVER['HTTP_REFERER'];
           }else{
             $from_url='';
           }
        $avatar =htmlspecialchars($arr2['avatar']);
        $groupid =htmlspecialchars($arr2['groupid']);

        if($visiter_name == ''){
            $visiter_name='游客'.$visiter_id;
        }
        $app_key = app_key;
        $whost = whost;
        $arr = parse_url($whost);
        if($arr['scheme'] == 'ws')
        {
            $port ='wsPort';
            $value ='false';
        }else
            {
            $value ='true';
            $port ='wssPort';
        }

        $business =User::table('wolive_business')->where('business_id',$business_id)->find();

        $this->assign("atype",$business['audio_state']);
        $this->assign('groupid',$groupid);
        $this->assign('app_key', $app_key);
        $this->assign('whost', $arr['host']);
        $this->assign('value', $value);
        $this->assign('wport', wport);
        $this->assign('port',$port);
        $this->assign('url',$url);
        $this->assign('visiter',$visiter_name);
        $this->assign('business_id',$business_id);
        $this->assign('from_url',$from_url);
        $this->assign('channel',$channel);
        $this->assign('visiter_id',$visiter_id);
        $this->assign('avatar',$avatar);
        return $this->fetch();
    }

}
